#include<iostream>
#include<sys/stat.h>
#include<fstream>

class FileHelper{
    FileHelper(std::string filename):_filename(filename){}
    //文件是否存在
    bool exist(){
        struct stat st;
        return ( stat(_filename.c_str(),&st) == 0 );
    }
    //求文件大小
    size_t size(){
        struct stat st;
        if( stat(_filename.c_str(),&st) != 0 ){
            return 0;
        }
        return st.st_size;
    }
    //指定位置读取指定长度文件
    bool read(char* body,size_t offset,size_t len){
        //以二进制模式打开文件
        std::ifstream ifs(_filename,std::ios::binary | std::ios::in);
        if(ifs.is_open() == false){
            return false;
        }
        //跳转到预读取位置
        ifs.seekg(offset,std::ios::beg);
        //开始读取数据
        ifs.read(body,len);
        if(ifs.good() == false){
            return false;
        }
        //关闭文件
        ifs.close();
    }
    //无脑从头读
    bool read(std::string& body){
        size_t fsize=this->size();
        body.resize(fsize);
        return read(&body[0],0,fsize);
    }
    //在文件指定位置写入指定长度数据
    bool write(const char* body,size_t offset,size_t len){
        //以写方式打开文件
        std::fstream fa(_filename,std::ios::binary | std::ios::in | std::ios::out);
        //让文件调转到指定位置
        fa.seekg(offset,std::ios::beg);
        //写入数据
        fa.write(body,len);

        if(fa.good() == false){
            return false;
        }
        fa.close();
        return true;
    }
    //无脑从头写
    bool write(std::string& body)
    {
        size_t len=body.size();
        return write(&body[0],0,len);
    }
    //重命名文件
    bool rename(const std::string &filename){
        return (::rename(_filename.c_str(),filename.c_str()) == 0 );
    }
    //删除文件
    bool removeFile(const std::string &filename){
        return (remove(_filename.c_str()) == 0);
    }
    //找到父目录
    static std::string parentDirectory(const std::string& filename)
    {
        size_t pos =filename.find_last_of("/");
        if(pos==std::string::npos){
            return "./";
        }
        std::string tmp_str=filename.substr(0,pos-0);
        return 0;
    }
    //创建文件
    static bool createFile(std::string& filename){
        std::fstream fs(filename,std::ios::binary | std::ios::out);
        if(fs.good() == false){
            return false;
        }
        fs.close();
        return true;
    }
    //创建目录
    static bool createDirectory( const std::string &path){
        int pos=0,idx=0;
        while(idx<path.size()){
            pos=path.find("/",idx);
            if(pos == std::string::npos){
                return (mkdir(path.c_str(),0775) == 0);
            }
            std::string directory_name = path.substr(0,pos-0);
            int ret=mkdir(directory_name.c_str(),0755);
            if(ret != 0 && errno != EEXIST){
                return false;
            }
            idx=pos+1;
        }
        return true;
    }
    //删除目录
    static bool removeDirectory(const std::string &path){
        std::string cmd="rm -rf"+path;
        return (system(cmd.c_str()) != -1);
    }
    private:
    std::string _filename;
};